﻿#include "../HDCDomeVideoPanel.h"
#include "../../include/HGlobalVariable.h"
#include "../../include/StdAfx.h"

HDCDomeVideoPanel::HDCDomeVideoPanel(QWidget *parent)
    :HBorderTwinklingWidget(parent)
{
    m_bChecked = false;
	m_bVideoPlaying = false;
	m_bVideoSelectEnable = true;

    m_bHasWarning = false;
    m_bHasNewWarning = false;

    m_posPressed = QPoint(0,0);
	QHash<QString,QVariant> hashData;
	hashData.insert(VIDEO,QVariant(VIDEOPANELDEFAULTDATA));
	hashData.insert(AUDIO,QVariant(DEFAULT));
	m_varData = QVariant(hashData);
    m_nCurrentBitStreamIndex = -1;
	m_pBottomHideTimer = new QTimer();
	m_pBottomHideTimer->setInterval(10000);
	connect(m_pBottomHideTimer,SIGNAL(timeout()),this,SLOT(sltHideBottom()));

    m_ppbWarning = NULL;

    initUI();
	setVideoPlay(false);
}

HDCDomeVideoPanel::~HDCDomeVideoPanel()
{

}

HWND HDCDomeVideoPanel::getVideoHWnd()
{
    return (HWND)m_plblVideo->winId();
}

QWidget *HDCDomeVideoPanel::getVideoWidget()
{
	return (QWidget*)m_plblVideo;
}

void HDCDomeVideoPanel::updateDisplay()
{
    m_plblVideo->repaint();
}

void HDCDomeVideoPanel::setChecked(bool bChecked)
{
	m_bChecked = bChecked;
	HBorderTwinklingWidget::setChecked(bChecked);
}

void HDCDomeVideoPanel::showBottomBar(bool bShow)
{
	if(bShow)
	{
		if(m_bVideoPlaying)
		{
			m_pwndBottom->show();
			m_pBottomHideTimer->start();
		}
	}
	else
	{
		m_pwndBottom->hide();
		m_pBottomHideTimer->stop();
	}
}

void HDCDomeVideoPanel::setResolutionVisible(bool bVisible)
{
    m_plblVideoResolution->setVisible(bVisible);
}

void HDCDomeVideoPanel::setData(QVariant varData)
{
	m_varData = varData;
}

QVariant HDCDomeVideoPanel::getData()
{
	return m_varData;
}

void HDCDomeVideoPanel::setVideoName(QString strVideoName)
{
	if(m_plblVideoName)
	{
		m_plblVideoName->setText(strVideoName);
	}
}

QString HDCDomeVideoPanel::getVideoName()
{
	if (m_plblVideoName)
	{
		return m_plblVideoName->text();
	}
}

void HDCDomeVideoPanel::setResolution(QString strResolution)
{
	if(m_plblVideoResolution)
	{
		m_plblVideoResolution->setText(strResolution);
	}
}

QString HDCDomeVideoPanel::getResolution()
{
	if (m_plblVideoResolution)
	{
		return m_plblVideoResolution->text();
	}
}

void HDCDomeVideoPanel::setBitSteamChecked(int nBitSteam)
{
	if(nBitSteam)
	{
		m_ppbSubBitStream->setChecked(true);
	}
	else
	{
		m_ppbMainBitStream->setChecked(true);
	}
}

int HDCDomeVideoPanel::getBitSteam()
{
	//具体返回值待定？？？？
	if (m_ppbMainBitStream->isChecked())
	{
		return 1;
	}
	else if(m_ppbSubBitStream->isChecked())
	{
		return 3;
	}
	else
	{
		return 0;
	}
}

void HDCDomeVideoPanel::setVideoPlay(bool bPlay)
{
	m_bVideoPlaying = bPlay;
	if(m_bVideoPlaying)
	{
		int nBitStream = m_varData.toHash().value(BITSTREAM).toInt();
		if(nBitStream == 3)
		{
			m_ppbMainBitStream->show();
			m_ppbSubBitStream->show();
		}
		else if(nBitStream == 2)
		{
			m_ppbSubBitStream->show();
			m_ppbSubBitStream->setChecked(true);
		}
		else if(nBitStream == 1)
		{
			m_ppbMainBitStream->show();
			m_ppbMainBitStream->setChecked(true);
		}
		m_plblVideoName->show();
	}
	else
	{
		m_ppbMainBitStream->hide();
		m_ppbSubBitStream->hide();
		m_plblVideoName->hide();
		m_plblVideoResolution->setText("");
		m_pwndBottom->hide();
		m_pBottomHideTimer->stop();
	}
}

void HDCDomeVideoPanel::setBroadCastEnable(bool bEnable)
{
	m_ppbBroadCast->setEnabled(bEnable);
}

void HDCDomeVideoPanel::setBroadCastChecked(bool bChecked)
{
    m_ppbBroadCast->setChecked(bChecked);
	m_ppbBroadCast->repaint();
}

void HDCDomeVideoPanel::setVideoSelectEnable(bool bEnable)
{
    m_bVideoSelectEnable = bEnable;
}

void HDCDomeVideoPanel::setWarningState(bool bWarning)
{
	emit sigVideoWarningStateChanged(m_varData.toHash(),bWarning);
	if(m_bHasNewWarning != bWarning)
	{
		m_bHasNewWarning = bWarning;
		setHasHistoryWarning(true);
		if(bWarning)
		{
			m_ppbWarning->setObjectName("dc_btn_warningInfo_warning");
			m_ppbWarning->setStyleSheet("QPushButton#dc_btn_warningInfo_warning\
										{\
										border-image:url(%1HIDI_WARNING_WARNING.png);\
										}\
										QPushButton#dc_btn_warningInfo_warning:pressed\
										{\
										border-image:url(%1HIDI_WARNING_PRESS.png);\
										}");
			m_ppbWarning->repaint();
			startTwinkling();
		}
		else
		{
			m_ppbWarning->setObjectName("dc_btn_warningInfo_nor");
			m_ppbWarning->setStyleSheet("QPushButton#dc_btn_warningInfo_nor\
										{\
										border-image:url(%1HIDI_WARNING_NOR.png);\
										}\
										QPushButton#dc_btn_warningInfo_nor:pressed\
										{\
										border-image:url(%1HIDI_WARNING_PRESS.png);\
										}");
			m_ppbWarning->repaint();
			stopTwinkling();
		}
	}
}

bool HDCDomeVideoPanel::isVideoPlaying()
{
	return m_bVideoPlaying;
}

QString HDCDomeVideoPanel::getDeviceIDFromVideoPanel(HDCDomeVideoPanel *wndVideoPanel)
{
	if (wndVideoPanel)
	{
		QHash<QString,QVariant> hashVideoData = wndVideoPanel->getData().toHash();
		QString strPanelData = hashVideoData.value(VIDEO).toString();
		QString strDeviceID = strPanelData.mid(0,strPanelData.indexOf(","));
		QString strTaskID = strPanelData.mid(strPanelData.indexOf(",") + 1,-1);

		QString deviceID = strDeviceID.remove(DIVICEID);
		QString taskID = strTaskID.remove(TASKID);

		return deviceID;
	}
}

void HDCDomeVideoPanel::copyVideoPanelValue(HDCDomeVideoPanel *pwndVideoPanel)
{
	m_bChecked = pwndVideoPanel->m_bChecked;
	m_bHasNewWarning = pwndVideoPanel->m_bHasNewWarning;
	m_bHasWarning = pwndVideoPanel->m_bHasWarning;
	setData(pwndVideoPanel->m_varData);
	m_nCurrentBitStreamIndex = pwndVideoPanel->m_nCurrentBitStreamIndex;
	m_pBottomHideTimer = pwndVideoPanel->m_pBottomHideTimer;
	m_bVideoPlaying = pwndVideoPanel->m_bVideoPlaying;
	m_bVideoSelectEnable = pwndVideoPanel->m_bVideoSelectEnable;

	m_posPressed = pwndVideoPanel->m_posPressed;	

	initUI();
	setVideoName(pwndVideoPanel->m_plblVideoName->text());
	m_plblVideo->setText(pwndVideoPanel->m_plblVideo->text());
	setResolution(pwndVideoPanel->m_plblVideoResolution->text());
	
	setVideoPlay(true);
	//*m_pwndBottom = *(pwndVideoPanel->m_pwndBottom);
}

bool HDCDomeVideoPanel::isVideoPanelChecked()
{
	return m_bChecked;
	//return m_bVideoSelectEnable;
}

void HDCDomeVideoPanel::setHasHistoryWarning(bool bHasHistoryWarning)
{
    m_bHasWarning = bHasHistoryWarning;
    if(bHasHistoryWarning)
    {
        m_ppbWarning->show();
    }
}
//void HDCDomeVideoPanel::setBitSteamBtnVisible(int nBitStream,bool bVisible)
//{
//	if(nBitStream == 1)
//	{
//		m_ppbMainBitStream->setVisible(bVisible);
//	}
//	else if(nBitStream == 2)
//	{
//		m_ppbSubBitStream->setVisible(bVisible);
//	}
//}

//void HDCDomeVideoPanel::paintEvent(QPaintEvent *event)
//{
//    QStyleOption opt;
//    opt.init(this);
//    QPainter p(this);
//    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//    QWidget::paintEvent(event);
//}

void HDCDomeVideoPanel::resizeEvent(QResizeEvent *event)
{
    m_pwndBottom->setFixedSize(this->width() - 4,g_nCameraControlBtnWidth + 4);
    m_pwndBottom->setGeometry(2,this->height() - m_pwndBottom->height()- 2,m_pwndBottom->width(),m_pwndBottom->height());
    QWidget::resizeEvent(event);
}

void HDCDomeVideoPanel::mousePressEvent(QMouseEvent *event)
{
	{
		emit sigPanelChecked();
	}
   // m_posPressed = event->pos();
    QWidget::mousePressEvent(event);
}

void HDCDomeVideoPanel::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->pos() == m_posPressed)
    {
      //  emit sigPanelChecked();
    }
    QWidget::mouseReleaseEvent(event);
}

void HDCDomeVideoPanel::mouseDoubleClickEvent(QMouseEvent *event)
{
	emit sigPanelDoubleClicked();
	QWidget::mouseDoubleClickEvent(event);
}

void HDCDomeVideoPanel::initUI()
{
    this->setObjectName("dc_wnd_videoPanel");
    m_pvblMain = new QVBoxLayout();
    m_pvblMain->setSpacing(0);
    m_pvblMain->setContentsMargins(2,2,2,2);

    m_phblTitle = new QHBoxLayout();
    m_phblTitle->setSpacing(8);
    m_phblTitle->setContentsMargins(10,2,10,2);

    m_phblBottom = new QHBoxLayout();
    m_phblBottom->setSpacing(15);
    m_phblBottom->setContentsMargins(20,0,0,0);


    m_pwndTitle = new QWidget(this);//1  total 22.5
    m_pwndTitle->setObjectName("dcVideoPanel_wnd_title");
	m_pwndTitle->setFixedHeight(g_nCameraControlBtnWidth + 2);

    m_plblVideoName = new QLabel(m_pwndTitle);
    m_plblVideoName->setObjectName("dc_lbl_sliderSpeed");
    m_plblVideoName->setText(QString::fromLocal8Bit(DEFAULTVIDEONAME));
	m_plblVideoName->setMinimumWidth(50);

    m_plblVideoResolution = new QLabel(m_pwndTitle);
    m_plblVideoResolution->setObjectName("dc_lbl_sliderSpeed");
    m_plblVideoResolution->setText(QString::fromLocal8Bit(DEFAULTRESOLUTION));

    m_pbgBitStream = new QButtonGroup(m_pwndTitle);
    m_pbgBitStream->setExclusive(true);

    m_ppbMainBitStream = new QPushButton();
    m_ppbMainBitStream->setObjectName("dc_btn_withText_small");
    m_ppbMainBitStream->setFixedSize(g_nCameraControlBtnWidth ,g_nCameraControlBtnWidth);
    m_ppbMainBitStream->setText(QString::fromLocal8Bit("1"));
    m_ppbMainBitStream->setCheckable(true);
	m_ppbMainBitStream->setChecked(true);
	m_ppbMainBitStream->setToolTip(QString::fromLocal8Bit("主码流"));
    m_pbgBitStream->addButton(m_ppbMainBitStream,0);

    m_ppbSubBitStream = new QPushButton();
    m_ppbSubBitStream->setObjectName("dc_btn_withText_small");
    m_ppbSubBitStream->setFixedSize(g_nCameraControlBtnWidth,g_nCameraControlBtnWidth);
    m_ppbSubBitStream->setText(QString::fromLocal8Bit("2"));
    m_ppbSubBitStream->setCheckable(true);
	m_ppbSubBitStream->setToolTip(QString::fromLocal8Bit("子码流"));
    m_pbgBitStream->addButton(m_ppbSubBitStream,1);


    m_plblVideo = new QLabel(this);
    m_plblVideo->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    m_plblVideo->setObjectName("dc_lbl_video");


    m_pwndBottom = new QWidget(this);
    m_pwndBottom->setObjectName("dcVideoPanel_wnd_bottom");

    m_ppbHangingUp = new QPushButton(m_pwndBottom);
    m_ppbHangingUp->setFixedSize(g_nCameraControlBtnWidth,g_nCameraControlBtnWidth);
    m_ppbHangingUp->setObjectName("dc_btn_hangOn");
	m_ppbHangingUp->setToolTip(QString::fromLocal8Bit("挂断"));
    m_ppbBroadCast = new QPushButton(m_pwndBottom);
    m_ppbBroadCast->setCheckable(true);
	m_ppbBroadCast->setChecked(false);
    m_ppbBroadCast->setFixedSize(g_nCameraControlBtnWidth,g_nCameraControlBtnWidth);
    m_ppbBroadCast->setObjectName("dc_btn_broadCast");
	m_ppbBroadCast->setToolTip(QString::fromLocal8Bit("广播"));

    m_ppbWarning = new QPushButton(m_pwndBottom);
    m_ppbWarning->setCheckable(false);
    m_ppbWarning->setFixedSize(g_nCameraControlBtnWidth,g_nCameraControlBtnWidth);
    m_ppbWarning->setObjectName("dc_btn_warningInfo_nor");
    m_ppbWarning->setToolTip(QString::fromLocal8Bit("告警信息"));
    m_ppbWarning->hide();


    m_phblTitle->addWidget(m_plblVideoName);
    m_phblTitle->addStretch(100);
    m_phblTitle->addWidget(m_plblVideoResolution);
    m_phblTitle->addWidget(m_ppbMainBitStream);
    m_phblTitle->addWidget(m_ppbSubBitStream);
    m_pwndTitle->setLayout(m_phblTitle);

    m_phblBottom->addWidget(m_ppbHangingUp);
    m_phblBottom->addWidget(m_ppbBroadCast);
    m_phblBottom->addWidget(m_ppbWarning);
    m_phblBottom->addStretch(50);
    m_pwndBottom->setLayout(m_phblBottom);
    m_pwndBottom->setFixedSize(this->width() - 4,g_nCameraControlBtnWidth + 4);
    m_pwndBottom->setGeometry(2,this->height() - m_pwndBottom->height() - 2,m_pwndBottom->width(),m_pwndBottom->height());
    m_pwndBottom->hide();

    m_pvblMain->addWidget(m_pwndTitle,1);
    m_pvblMain->addWidget(m_plblVideo,21);

    this->setLayout(m_pvblMain);

    connect(m_ppbHangingUp,SIGNAL(clicked()),this,SLOT(sltCloseCurrentVideo()));
    connect(m_ppbBroadCast,SIGNAL(clicked()),this,SLOT(sltBroadCaseBtnClicked()));
    connect(m_ppbWarning,SIGNAL(clicked()),this,SLOT(sltWarningBtnClicked()));

    connect(m_pbgBitStream,SIGNAL(buttonClicked(int)),this,SLOT(sltBitStreamBtnClicked(int)));
}

void HDCDomeVideoPanel::sltBroadCaseBtnClicked()
{
    if(m_ppbBroadCast->isChecked())
    {
		emit sigOpenCurrentVideoBroadCast();
    }
    else
    {
		emit sigCloseCurrentBroadCast();
    }
}

void HDCDomeVideoPanel::sltWarningBtnClicked()
{
    emit sigViewWarningInfo();
}

void HDCDomeVideoPanel::sltBitStreamBtnClicked(int nBitStreamIndex)
{
    if(m_nCurrentBitStreamIndex != nBitStreamIndex)
    {
        emit sigBitStreamChanged(nBitStreamIndex);
        m_nCurrentBitStreamIndex = nBitStreamIndex;
    }

}

void HDCDomeVideoPanel::sltHideBottom()
{
	m_pwndBottom->hide();
	m_pBottomHideTimer->stop();
}

void HDCDomeVideoPanel::sltCloseCurrentVideo()
{
	emit sigCloseVideo(this);
}

